package org.fireflow2.designer.eclipse.modelwrapper.basic;

import java.util.List;

import org.fireflow.model.process.WorkflowElement;
import org.fireflow.pdl.fpdl20.diagram.ActivityShape;
import org.fireflow.pdl.fpdl20.diagram.DiagramElement;
import org.fireflow.pdl.fpdl20.diagram.Fpdl20DiagramElement;
import org.fireflow.pdl.fpdl20.diagram.StartNodeShape;
import org.fireflow.pdl.fpdl20.diagram.WorkflowNodeShape;
import org.fireflow.pdl.fpdl20.process.Activity;
import org.fireflow.pdl.fpdl20.process.StartNode;
import org.fireflow.pdl.fpdl20.process.SubProcess;
import org.fireflow2.designer.eclipse.modelwrapper.ModelWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.NodeWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.SubProcessWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.Wrapper;

public class ActivityWrapper extends NodeWrapper {	
	public static final String Name = "Name";
	public ActivityWrapper(SubProcessWrapper subflowWrapper,Activity arg0,DiagramElement arg1){
		super(subflowWrapper,arg0,arg1);
		initChildren();
	}

	private void initChildren(){
		ActivityShape activityShape = (ActivityShape)this.getDiagramElement();
		List<StartNodeShape> attachedStartNodeShapeList = activityShape.getAttachedStartNodeShapes();
		SubProcess subflow = (SubProcess)this.subProcessWrapper.getWorkflowModelElement();
		
		for (StartNodeShape startNodeShape : attachedStartNodeShapeList){
			String wfElmID = startNodeShape.getWorkflowElementRef();
			
			WorkflowElement wfElm = subflow.findWFElementById(wfElmID);
			ModelWrapper modelWrapper = null;

			modelWrapper = new StartNodeWrapper(subProcessWrapper,(StartNode)wfElm,startNodeShape);
			subProcessWrapper.getStartNodesWrapper().addChild((StartNodeWrapper)modelWrapper);
			modelWrapper.setParent(this);
			this.getChildren().add(modelWrapper);
			subProcessWrapper.putToSnModelWrapperMap(modelWrapper.getSn(), modelWrapper);					

		}

	}

	public void addChild(Wrapper child,int index){
		if (!(child instanceof StartNodeWrapper)){
			return;
		}
		((StartNodeWrapper)child).setAttachedToWrapperId(this.getID());
		
		child.setParent(this);
		
		SubProcess subflow = (SubProcess)getSubProcessWrapper().getWorkflowModelElement();
		ActivityShape groupShape = (ActivityShape)this.getDiagramElement();
		Activity activity = (Activity)this.getWorkflowModelElement();
		
		
		StartNode node = (StartNode)((StartNodeWrapper)child).getWorkflowModelElement();
		
		subflow.getStartNodes().add(node);
		activity.getAttachedStartNodes().add(node);

		groupShape.addAttachedStartNodeShape((StartNodeShape)((ModelWrapper) child).getDiagramElement());
		subProcessWrapper.getStartNodesWrapper().addChild(child);
		subProcessWrapper.putToSnModelWrapperMap(child.getSn(), child);
		
		super.addChild(child,index);
	}
	
	public void deleteChild(ModelWrapper child){
		child.setParent(null);
		SubProcess subflow = (SubProcess)subProcessWrapper.getWorkflowModelElement();
		ActivityShape groupShape = (ActivityShape)this.getDiagramElement();
		StartNode node = (StartNode)((StartNodeWrapper)child).getWorkflowModelElement();
		subflow.getStartNodes().remove(node);
		Activity activity = (Activity)this.getWorkflowModelElement();
		activity.getAttachedStartNodes().remove(node);
		groupShape.getAttachedStartNodeShapes().remove((WorkflowNodeShape)((ModelWrapper) child).getDiagramElement());
		
		
		subProcessWrapper.getStartNodesWrapper().deleteChild(child);
		subProcessWrapper.removeFromSnModelWrapperMap(child.getSn());
		super.deleteChild(child);
	}
	
	public void setCompleteStrategy(String s){
//		Activity activity = (Activity)this.wfElement;
//		String oldValue = activity.getCompletionStrategy();
//		activity.setCompletionStrategy(s);
//		this.firePropertyChange(WF_ELEMENT_ATTR, oldValue, s);
	}
	
	public String getCompleteStrategy(){
		return null;//TODO 2.0 待处理
//		Activity activity = (Activity)this.wfElement;
//		String s = activity.getCompletionStrategy();
//		return s;
	}

//	public List getInlineTaskWrapperList() {
//		return inlineTaskWrapperList;
//	}
//
//	public void setInlineTaskWrapperList(List inlineTaskWrapperList) {
//		this.inlineTaskWrapperList = inlineTaskWrapperList;
//	}
//
//	public List getTaskRefWrapperList() {
//		return taskRefWrapperList;
//	}
//
//	public void setTaskRefWrapperList(List taskRefWrapperList) {
//		this.taskRefWrapperList = taskRefWrapperList;
//	}
	
	public  String getElementType(){
		return Fpdl20DiagramElement.ACTIVITY.name();
	}		
	
//	public boolean isTheNameConflict(String newName){
//		WorkflowProcess process = (WorkflowProcess)this.getParent().getWorkflowModelElement();
//		List activities = process.getActivities();
//		boolean conflict = false;
//		for (int i=0;i<activities.size();i++){
//			Activity activity = (Activity)activities.get(i);
//			if (activity.getName().equalsIgnoreCase(newName)){
//				conflict = true;
//				break;
//			}
//		}
//		return conflict;
//	}
}
